# AMAR ET AL. - COUNTERING MISINFORMATION EARLY (2025)
## REPLICATION FILE: 12_robust_multiple_hyp.R
### This script tests for robustness to multiple hypothesis corrections.
# ----
# Standard corrections ----
## Main Effects (ITT) ----
regressions_multiple_hyp <- regressions %>%
  filter(type == "idx" & secondary_outcome == F, mechanism == F) %>%
  mutate(
    p.value_bh = p.adjust(p.value, method = 'BH', n = length(p.value)),
    p.value_bonferroni = p.adjust(p.value, method = 'bonferroni', n = length(p.value)))

regressions_multiple_hyp %>%
  select(label, n, estimate, std.error, p.value, p.value_bh, p.value_bonferroni) %>%
  mutate(label = str_remove_all(label, " Index"),
         n = format(n, big.mark = ","),
         estimate = format(round(estimate, 2), nsmall = 2),
         std.error = round(std.error, 3),
         p.value = case_when(p.value > 0.9 ~ "$>$0.9",
                             p.value < 0.001 ~ "$<$0.001",
                             p.value < 0.01 ~ as.character(round(p.value, 3)),
                             TRUE ~ as.character(round(p.value, 2))),
         p.value_bh = case_when(p.value_bh > 0.9 ~ "$>$0.9",
                                p.value_bh < 0.001 ~ "$<$0.001",
                                p.value_bh < 0.01 ~ as.character(round(p.value_bh, 3)),
                                TRUE ~ as.character(round(p.value_bh, 2))),
         p.value_bonferroni = case_when(p.value_bonferroni > 0.9 ~ "$>$0.9",
                                        p.value_bonferroni < 0.001 ~ "$<$0.001",
                                        p.value_bonferroni < 0.01 ~ as.character(round(p.value_bonferroni, 3)),
                                        TRUE ~ as.character(round(p.value_bonferroni, 2)))) %>%
  rename(Outcome = label,
         N = n,
         Estimate = estimate,
         SE = std.error,
         "p" = p.value,
         "p (FDR)" = p.value_bh,
         "p (Bonf.)" = p.value_bonferroni) %>%
  xtable::xtable(caption = "Main results (ITT): correcting for multiple hypotheses", align = "llllllcc", digits = 3,
                 label = "tab:multiple_hyp_main_itt") %>%
  print(include.rownames = FALSE, 
        caption.placement = "top",
        sanitize.text.function = identity, # add backslashes to special characters
        sanitize.colnames.function = \(x) str_c("\\textbf{", x, "}"),
        add.to.row = list(
          pos = list(nrow(.), nrow(.)), # Two lines added at the end of the table
          command = c(
            "\\hline\n\\multicolumn{7}{l}{\\footnotesize P-values adjusted for False Discovery Rate (FDR) and using Bonferroni correction.} \\\\\n",
            "\\multicolumn{7}{l}{\\footnotesize DV: Main Indices. Models include library-spillover FEs.} \\\\\n"
          )),
        hline.after = c(-1,0), # add horizontal lane after second to last column
        comment = FALSE,
        table.placement = getOption("xtable.table.placement", "h!"),
        file = paste0("output/tables/tab_multiple_hyp_main_itt", ".tex"))

## Main Effects (CACE) ----
regressions_cace_multiple_hyp <- regressions_cace %>%
  filter(type == "idx" & secondary_outcome == F, mechanism == F) %>%
  mutate(
    p.value_bh = p.adjust(p.value, method = 'BH', n = length(p.value)),
    p.value_bonferroni = p.adjust(p.value, method = 'bonferroni', n = length(p.value)))

regressions_cace_multiple_hyp %>%
  select(label, n, estimate, std.error, p.value, p.value_bh, p.value_bonferroni) %>%
  mutate(label = str_remove_all(label, " Index"),
         n = format(n, big.mark = ","),
         estimate = format(round(estimate, 2), nsmall = 2),
         std.error = round(std.error, 3),
         p.value = case_when(p.value > 0.9 ~ "$>$0.9",
                             p.value < 0.001 ~ "$<$0.001",
                             p.value < 0.01 ~ as.character(round(p.value, 3)),
                             TRUE ~ as.character(round(p.value, 2))),
         p.value_bh = case_when(p.value_bh > 0.9 ~ "$>$0.9",
                                p.value_bh < 0.001 ~ "$<$0.001",
                                p.value_bh < 0.01 ~ as.character(round(p.value_bh, 3)),
                                TRUE ~ as.character(round(p.value_bh, 2))),
         p.value_bonferroni = case_when(p.value_bonferroni > 0.9 ~ "$>$0.9",
                                        p.value_bonferroni < 0.001 ~ "$<$0.001",
                                        p.value_bonferroni < 0.01 ~ as.character(round(p.value_bonferroni, 3)),
                                        TRUE ~ as.character(round(p.value_bonferroni, 2)))) %>%
  rename(Outcome = label,
         N = n,
         Estimate = estimate,
         SE = std.error,
         "p" = p.value,
         "p (FDR)" = p.value_bh,
         "p (Bonf.)" = p.value_bonferroni) %>%
  xtable::xtable(caption = "Main results (CACE): correcting for multiple hypotheses", align = "llllllll", digits = 3,
                 label = "tab:multiple_hyp_main_cace") %>%
  print(include.rownames = FALSE, 
        caption.placement = "top",
        sanitize.text.function = identity, # add backslashes to special characters
        sanitize.colnames.function = \(x) str_c("\\textbf{", x, "}"),
        add.to.row = list(
          pos = list(nrow(.), nrow(.)), # Two lines added at the end of the table
          command = c(
            "\\hline\n\\multicolumn{7}{l}{\\footnotesize P-values adjusted for False Discovery Rate (FDR) and using Bonferroni correction.} \\\\\n",
            "\\multicolumn{7}{l}{\\footnotesize DV: Main Indices. Models include library-spillover FEs.} \\\\\n"
          )),
        hline.after = c(-1,0), # add horizontal lane after second to last column
        comment = FALSE,
        table.placement = getOption("xtable.table.placement", "h!"),
        file = paste0("output/tables/tab_multiple_hyp_main_cace", ".tex"))

# Index of Indices ----
## Endline ----
index_of_indices_mod <- svyglm(index_of_indices ~ treatment + library_spillover_pre, design = bimli_svy_dkr.rm)

index_of_indices_tidy <- index_of_indices_mod %>%
  tidy() %>%
  mutate(dv = "Summary Index of Indices", n = nobs(index_of_indices_mod),
         label = "Index of Indices") %>%
  filter(term == "treatmentMedia Literacy")

index_of_indices_tidy %>%
  #filter(idx == type & secondary_outcome == FALSE) %>% # filter to variables that are included in the index
  select(label, n, estimate, std.error, p.value) %>%
  mutate(n = format(n, big.mark = ","),
         estimate = format(round(estimate, 2), nsmall = 2),
         std.error = round(std.error, 3),
         p.value = case_when(p.value > 0.9 ~ "$>$0.9",
                             p.value < 0.001 ~ "$<$0.001",
                             p.value < 0.01 ~ as.character(round(p.value, 3)),
                             TRUE ~ as.character(round(p.value, 2)))) %>%
  rename(Outcome = label,
         N = n,
         Estimate = estimate,
         SE = std.error,
         "p-value" = p.value) %>%
  xtable::xtable(caption = "ITT effects for summary index of indices", align = "llllll", digits = 3,
                 label = "tab:index_of_indices") %>%
  print(include.rownames = FALSE, 
        caption.placement = "top",
        sanitize.text.function = identity, # add backslashes to special characters
        sanitize.colnames.function = \(x) str_c("\\textbf{", x, "}"),
        add.to.row = list(
          pos = list(nrow(.), nrow(.)), # Two lines added at the end of the table
          command = c(
            "\\hline\n\\multicolumn{5}{l}{\\footnotesize ITT effect estimates for index of indices.} \\\\\n",
            "\\multicolumn{5}{l}{\\footnotesize DV: standardized, ICW-weighted index of main indices.} \\\\\n"
          )),
        hline.after = c(-1,0), # add horizontal lane after second to last column
        comment = FALSE,
        table.placement = getOption("xtable.table.placement", "h!"),
        file = paste0("output/tables/tab_index_of_indices_itt", ".tex"))

## Follow-up ----
index_of_indices_follow_mod <- svyglm(follow_index_of_indices ~ treatment + library_spillover_pre, design = bimli_svy_dkr.rm)

index_of_indices_follow_tidy <- index_of_indices_follow_mod %>%
  tidy() %>%
  mutate(dv = "Summary Index of Indices", n = nobs(index_of_indices_follow_mod),
         label = "Index of Indices") %>%
  filter(term == "treatmentMedia Literacy")

index_of_indices_follow_tidy %>%
  select(label, n, estimate, std.error, p.value) %>%
  mutate(n = format(n, big.mark = ","),
         estimate = format(round(estimate, 2), nsmall = 2),
         std.error = round(std.error, 3),
         p.value = case_when(p.value > 0.9 ~ "$>$0.9",
                             p.value < 0.001 ~ "$<$0.001",
                             p.value < 0.01 ~ as.character(round(p.value, 3)),
                             TRUE ~ as.character(round(p.value, 2)))) %>%
  rename(Outcome = label,
         N = n,
         Estimate = estimate,
         SE = std.error,
         "p-value" = p.value) %>%
  xtable::xtable(caption = "Second endline ITT effects for summary index of indices", align = "llllll", digits = 3,
                 label = "tab:index_of_indices_follow") %>%
  print(include.rownames = FALSE, 
        caption.placement = "top",
        sanitize.text.function = identity, # add backslashes to special characters
        sanitize.colnames.function = \(x) str_c("\\textbf{", x, "}"),
        add.to.row = list(
          pos = list(nrow(.), nrow(.)), # Two lines added at the end of the table
          command = c(
            "\\hline\n\\multicolumn{5}{l}{\\footnotesize ITT effect estimates for index of indices.} \\\\\n",
            "\\multicolumn{5}{l}{\\footnotesize DV: standardized, ICW-weighted index of main indices.} \\\\\n"
          )),
        hline.after = c(-1,0), # add horizontal lane after second to last column
        comment = FALSE,
        table.placement = getOption("xtable.table.placement", "h!"),
        file = paste0("output/tables/tab_index_of_indices_follow_itt", ".tex"))

# END of 12_robust_multiple_hyp.R ----